//
//  Button.m
//  LotteryClient
//
//  Created by Suny on 14-8-2.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#define kHighlightAlpha             0.3
#define kNormalAlpha                1

#import "Button.h"

@implementation Button
@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.exclusiveTouch = YES;
        [self addTargets];
    }
    return self;
}

- (id)init
{
    self = [super init];
    if (self) {
        [self addTargets];
    }
    return self;
}

+ (id)buttonWithType:(UIButtonType)buttonType
{
    Button * alphaButton = [super buttonWithType:buttonType];
    [alphaButton addTargets];
    return alphaButton;
}

- (void)addTargets
{
    self.adjustsImageWhenHighlighted = NO;
    [self addTarget:self action:@selector(highlight:) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(normal:) forControlEvents:UIControlEventTouchUpOutside];
    [self addTarget:self action:@selector(normal:) forControlEvents:UIControlEventTouchUpInside];
    [self addTarget:self action:@selector(normal:) forControlEvents:UIControlEventTouchCancel];
}

- (void)highlight:(Button *)button{
    button.alpha = kHighlightAlpha;
    if ([self.delegate respondsToSelector:@selector(highlightAlphaButton:)]) {
        [self.delegate highlightAlphaButton:button];
    }
}

- (void)normal:(Button *)button{
    button.alpha = kNormalAlpha;
    if ([self.delegate respondsToSelector:@selector(normalAlphaButton:)]) {
        [self.delegate normalAlphaButton:button];
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */
@end

